home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-04 | 11.5 KB | 348 lines |
-
- package sub_arctic.lib;
-
- import sub_arctic.input.event;
- import sub_arctic.input.pressable;
- import sub_arctic.input.simple_draggable;
- import sub_arctic.input.callback_object;
- import sub_arctic.input.int_holder;
-
- import sub_arctic.output.loaded_image;
-
- import java.awt.Image;
-
- /**
- * This class creates a slider with either the default or other
- * look. It handles all the input side processing and then uses
- * h_slider_display (from which it is derived) to do the display.
- *
- * @author Scott Hudson
- */
- public class h_slider
- extends h_slider_display implements pressable, simple_draggable {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- *Object we make callbacks to (if non-null)
- */
- protected callback_object _callback_obj;
-
- /**
- * Object that this slider makes callbacks to when moved. There are two
- * callbacks made. Number 0 is made dynamically as the slider moves,
- * while number 1 is made only once the slider is released. In both cases
- * the callback_info contains an Integer object with the value of the slider.
- * @return callback_object the object we make callbacks to
- */
- public callback_object callback_obj() {return _callback_obj;}
-
- /**
- * Set the callback object that this button makes callbacks to when moved.
- * There are two callbacks made. Number 0 is made dynamically as the slider
- * moves, while number 1 is made only once the slider is released. In both
- * cases the callback_info contains an Integer object with the value of the
- * slider.
- *
- * @param callback_object cb the new object to which we make callbacks.
- */
- public void set_callback_obj(callback_object cb) {_callback_obj = cb;}
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Create a new horizontal slider (not scrollbar). This is the full
- * constructor interface with all the possible options.
- *
- * @param int xv x position
- * @param int yv y position
- * @param int wv width in pixels
- * @param int minv minimum slider value
- * @param int maxv maximum slider value
- * @param int init_val initial slider value.
- * @param int sincv small increment value (when the left
- * or right buttons are pushed).
- * @param int lincv large increment value (the user clicks
- * in the thumb area but not on the thumb).
- * @param callback_object call_obj the object which receives the
- * callbacks (see above).
- * @param loaded_image rt_im image for right button.
- * @param loaded_image lf_im image for left button.
- * @param loaded_image th_im image for thumb.
- * @param loaded_image back_pat image for the background pattern.
- */
- public h_slider(
- int xv, int yv, int wv,
- int minv, int maxv,
- int init_val,
- int sincv, int lincv,
- callback_object call_obj,
- loaded_image rt_im, loaded_image lf_im, loaded_image th_im,
- loaded_image back_pat)
- {
- /* let the superclass do most of the work */
- super(xv,yv,wv,minv,maxv,init_val,sincv,lincv,rt_im,lf_im,th_im,back_pat);
-
- /* initialize additional locals */
- _callback_obj = call_obj;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Construct a horizontal slider (not a scrollbar) with the default
- * (Artkit) appearance.
- *
- * @param int xv x position.
- * @param int yv y position.
- * @param int wv width in pixels.
- * @param int minv minimum slider value.
- * @param int maxv maximum slider value.
- * @param int init_val initial slider value.
- * @param int sincv small increment value (when the left or
- * right buttons are pushed).
- * @param int lincv large increment value (the user clicks in
- * the thumb area but not on the thumb).
- * @param callback_object call_obj the object which receives the callbacks
- * (see above).
- */
- public h_slider(
- int xv, int yv, int wv,
- int minv, int maxv,
- int init_val,
- int sincv, int lincv,
- callback_object call_obj)
- {
- this(xv,yv,wv,minv,maxv,init_val,sincv,lincv, call_obj,
- null, null, null, null);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Constant for dynamic callback (made at each movement) */
- public static final int DYNAMIC_CALLBACK = 0;
-
- /** Constant for static callback (made only at end of movement) */
- public static final int STATIC_CALLBACK = 1;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Do a "dynamic" callback. This is callback that gets used
- * while the thumb of the slider is being dragged.
- *
- * @param event evt the event which caused this callback
- */
- protected void dynamic_callback(event evt)
- {
- if (callback_obj() != null)
- callback_obj().callback(this,evt,DYNAMIC_CALLBACK,new Integer(value()));
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Do a "static" callback (#1). This is the callback when the
- * thumb is "dropped" (and the mouse released). You should use
- * this callback exclusively if you don't want the dragged objects
- * to move as the scrollbar is moved but only when the thumb is
- * released.
- *
- * @param event evt the event which caused this callback
- */
- protected void static_callback(event evt)
- {
- if (callback_obj() != null)
- callback_obj().callback(this,evt,STATIC_CALLBACK,new Integer(value()));
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Handle mouse presses in our bounds -- makes us the drag focus if they
- * click in the thumb.
- *
- * @param event evt the press event (mouse down).
- * @param Object user_info the information that was handed to the
- * pick_collector when this object was deemed to be
- * picked (this value is given to the drag focus
- * agent also, so it will be propagated along to the
- * drag calls below).
- * @return boolean true if we consumed this event, which we will
- */
- public boolean press(event evt, Object user_info)
- {
- int off;
-
- /* in left-button region? */
- if (evt.local_x() <= _left_img.width()) {
- /* do a negative small increment */
- set_value(value() - small_inc());
-
- /* do callback */
- dynamic_callback(evt);
- static_callback(evt);
-
- return true;
- }
-
- /* in right-button region? */
- if (evt.local_x() >= w() - _right_img.width()) {
- /* do a small positive increment */
- set_value(value() + small_inc());
-
- /* do callback */
- dynamic_callback(evt);
- static_callback(evt);
-
- return true;
- }
-
- /* compute the offset to the thumb */
- off = thumb_offset();
-
- /* if there is no thumb, bail out now */
- if (off < 0) return false;
-
- /* before thumb? */
- if (evt.local_x() < _left_img.width()+off) {
- /* do a large negative increment */
- set_value(value() - large_inc());
-
- /* do callback */
- dynamic_callback(evt);
- static_callback(evt);
-
- return true;
- }
-
- /* in the thumb? */
- if (evt.local_x() < _left_img.width()+off+_thumb_img.width()) {
- /* make us the drag focus to track the thumb */
- manager.simple_drag_focus.set_focus_to(this,evt,new int_holder());
- return true;
- }
-
- /* must be below the thumb, do a large positive increment */
- set_value(value() + large_inc());
-
- /* do callback */
- dynamic_callback(evt);
- static_callback(evt);
-
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Companion for press() -- ignore here, it never gets called because
- * we become the drag focus.
- *
- * @param event evt the release event.
- * @param Object user_info the information that was handed to the
- * pick_collector when this object was deemed to be
- * picked.
- */
- public boolean release(event evt, Object user_info)
- {
- return false;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Start of a drag for the thumb.
- * @param event evt the event that is starting the drag (usually a
- * press).
- * @param Object user_info the object passed to the simple drag_agent
- */
- public boolean drag_start(event evt, Object user_info)
- {
- int off;
-
- /* remember how far away from top of thumb the event is */
- off = thumb_offset();
- ((int_holder)user_info).value = evt.local_x() - _left_img.width() - off;
-
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Movement within a drag of the thumb.
- *
- * @param event evt the drag event (mouse move).
- * @param Object user_info the object passed to the simple drag_agent.
- */
- public boolean drag_feedback(event evt, Object user_info)
- {
- int off, slide_range;
- int value_range, old_value;
-
- /* hold on to old value for a second */
- old_value = value();
-
- /* determine new top of thumb */
- off = evt.local_x() - _left_img.width() - ((int_holder)user_info).value;
-
- /* determine new value from this */
- slide_range = w() - _left_img.width() -
- _right_img.width() - _thumb_img.width();
- /* ok, if there is no way to move the slider, don't bother doing
- any more work */
- if (slide_range==0) {
- /* no more work to do */
- return true;
- }
- /* get the range of values and set the value of the slider */
- value_range = max_val() - min_val();
- set_value(min_val() + (off * value_range)/slide_range);
-
- /* if this is a new value, cause a redraw of us and do the callback */
- if (value() != old_value) {
- dynamic_callback(evt);
- }
-
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * End the simple drag of the thumb.
- *
- * @param event evt the end of the drag event (mouse release usually).
- * @param Object user_info the object passed to the simple drag_agent.
- */
- public boolean drag_end(event evt, Object user_info)
- {
- /* let drag_feedback do most of the work */
- drag_feedback(evt, user_info);
-
- /* do the static callback */
- static_callback(evt);
-
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-